<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>2023-08-31 学习笔记 | David 的博客</title><meta name="author" content="David"><meta name="copyright" content="David"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="2023-08-31 学习笔记1. 预训练语言模型的使用问题：如何使用预训练的语言模型在已有的数据集上进行微调？ 解决方案如下： 首先下载一个预训练语言模型，这里使用的是 cardiffnlp&#x2F;twitter-xlm-roberta-base-sentiment-multilingual 可以直接在Hugging Face 上下载 预处理数据集(很关键！！)，数据集如果没有处理好，模型运行过程中容">
<meta property="og:type" content="article">
<meta property="og:title" content="2023-08-31 学习笔记">
<meta property="og:url" content="https://blog.david-deng.cn/2023/08/31/2023-08-31%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="David 的博客">
<meta property="og:description" content="2023-08-31 学习笔记1. 预训练语言模型的使用问题：如何使用预训练的语言模型在已有的数据集上进行微调？ 解决方案如下： 首先下载一个预训练语言模型，这里使用的是 cardiffnlp&#x2F;twitter-xlm-roberta-base-sentiment-multilingual 可以直接在Hugging Face 上下载 预处理数据集(很关键！！)，数据集如果没有处理好，模型运行过程中容">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-7.png">
<meta property="article:published_time" content="2023-08-31T15:12:39.000Z">
<meta property="article:modified_time" content="2023-11-16T13:34:06.000Z">
<meta property="article:author" content="David">
<meta property="article:tag" content="笔记">
<meta property="article:tag" content="Pytorch">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-7.png"><link rel="shortcut icon" href="/img/favicon.svg"><link rel="canonical" href="https://blog.david-deng.cn/2023/08/31/2023-08-31%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>
    (() => {
      
    const saveToLocal = {
      set: (key, value, ttl) => {
        if (!ttl) return
        const expiry = Date.now() + ttl * 86400000
        localStorage.setItem(key, JSON.stringify({ value, expiry }))
      },
      get: key => {
        const itemStr = localStorage.getItem(key)
        if (!itemStr) return undefined
        const { value, expiry } = JSON.parse(itemStr)
        if (Date.now() > expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return value
      }
    }

    window.btf = {
      saveToLocal,
      getScript: (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        Object.entries(attr).forEach(([key, val]) => script.setAttribute(key, val))
        script.onload = script.onreadystatechange = () => {
          if (!script.readyState || /loaded|complete/.test(script.readyState)) resolve()
        }
        script.onerror = reject
        document.head.appendChild(script)
      }),
      getCSS: (url, id) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onload = link.onreadystatechange = () => {
          if (!link.readyState || /loaded|complete/.test(link.readyState)) resolve()
        }
        link.onerror = reject
        document.head.appendChild(link)
      }),
      addGlobalFn: (key, fn, name = false, parent = window) => {
        if (!false && key.startsWith('pjax')) return
        const globalFn = parent.globalFn || {}
        globalFn[key] = globalFn[key] || {}
        globalFn[key][name || Object.keys(globalFn[key]).length] = fn
        parent.globalFn = globalFn
      }
    }
  
      
      const activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      const activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }

      btf.activateDarkMode = activateDarkMode
      btf.activateLightMode = activateLightMode

      const theme = saveToLocal.get('theme')
    
          const mediaQueryDark = window.matchMedia('(prefers-color-scheme: dark)')
          const mediaQueryLight = window.matchMedia('(prefers-color-scheme: light)')
          
          if (theme === undefined) {
            if (mediaQueryLight.matches) activateLightMode()
            else if (mediaQueryDark.matches) activateDarkMode()
            else {
              const hour = new Date().getHours()
              const isNight = hour <= 6 || hour >= 18
              isNight ? activateDarkMode() : activateLightMode()
            }
            mediaQueryDark.addEventListener('change', () => {
              if (saveToLocal.get('theme') === undefined) {
                e.matches ? activateDarkMode() : activateLightMode()
              }
            })
          } else {
            theme === 'light' ? activateLightMode() : activateDarkMode()
          }
        
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        document.documentElement.classList.toggle('hide-aside', asideStatus === 'hide')
      }
    
      
    const detectApple = () => {
      if (/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)) {
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
  
    })()
  </script><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false,"highlightFullpage":false,"highlightMacStyle":false},
  copy: {
    success: '复制成功',
    error: '复制失败',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: David","link":"链接: ","source":"来源: David 的博客","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'null',
  Snackbar: {"chs_to_cht":"已切换为繁体中文","cht_to_chs":"已切换为简体中文","day_to_night":"已切换为深色模式","night_to_day":"已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: true,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '2023-08-31 学习笔记',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  isShuoshuo: false
}</script><style type="text/css">#toggle-sidebar {bottom: 80px}</style><meta name="generator" content="Hexo 7.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img text-center"><img src="/img/avatar.png" onerror="onerror=null;src='/img/loading.gif'" alt="avatar"/></div><div class="site-data text-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">28</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">28</div></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><span class="site-page group"><i class="fa-fw fas fa-compass"></i><span> 目录</span><i class="fas fa-chevron-down"></i></span><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url(https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-7.png);"><nav id="nav"><span id="blog-info"><a class="nav-site-title" href="/"><span class="site-name">David 的博客</span></a><a class="nav-page-title" href="/"><span class="site-name">2023-08-31 学习笔记</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><span class="site-page group"><i class="fa-fw fas fa-compass"></i><span> 目录</span><i class="fas fa-chevron-down"></i></span><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><span class="site-page"><i class="fas fa-bars fa-fw"></i></span></div></div></nav><div id="post-info"><h1 class="post-title">2023-08-31 学习笔记</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-08-31T15:12:39.000Z" title="发表于 2023-08-31 23:12:39">2023-08-31</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-11-16T13:34:06.000Z" title="更新于 2023-11-16 21:34:06">2023-11-16</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%AC%94%E8%AE%B0/">笔记</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%AC%94%E8%AE%B0/Pytorch/">Pytorch</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">总字数:</span><span class="word-count">2.8k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>14分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title=""><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">浏览量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="container post-content" id="article-container"><div id="post-outdate-notice" data="{&quot;limitDay&quot;:90,&quot;messagePrev&quot;:&quot;文章距离最近一次更新已经&quot;,&quot;messageNext&quot;:&quot;天，文章的内容可能已经过期。&quot;,&quot;postUpdate&quot;:&quot;2023-11-16 21:34:06&quot;}" hidden></div><h1 id="2023-08-31-学习笔记"><a href="#2023-08-31-学习笔记" class="headerlink" title="2023-08-31 学习笔记"></a>2023-08-31 学习笔记</h1><h2 id="1-预训练语言模型的使用"><a href="#1-预训练语言模型的使用" class="headerlink" title="1. 预训练语言模型的使用"></a>1. 预训练语言模型的使用</h2><h3 id="问题："><a href="#问题：" class="headerlink" title="问题："></a>问题：</h3><p><code>如何使用预训练的语言模型在已有的数据集上进行微调？</code></p>
<h3 id="解决方案如下："><a href="#解决方案如下：" class="headerlink" title="解决方案如下："></a>解决方案如下：</h3><ol>
<li>首先下载一个预训练语言模型，这里使用的是 <code>cardiffnlp/twitter-xlm-roberta-base-sentiment-multilingual</code> 可以直接在<a target="_blank" rel="noopener external nofollow noreferrer" href="https://huggingface.co/cardiffnlp/twitter-xlm-roberta-base-sentiment-multilingual">Hugging Face</a> 上下载</li>
<li>预处理数据集(很关键！！)，数据集如果没有处理好，模型运行过程中容易报错，例如 <code>3. 错误解决</code> 中就出现了因为数据集没有处理好而导致的错误</li>
<li>加载模型和分词器</li>
<li>模型训练</li>
<li>模型评估</li>
</ol>
<h3 id="代码如下："><a href="#代码如下：" class="headerlink" title="代码如下："></a>代码如下：</h3><ol>
<li><p>项目配置文件</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># hydra 配置</span></span><br><span class="line"><span class="attr">defaults:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">_self_</span> <span class="comment"># 优先级最高, 会覆盖其他配置</span></span><br><span class="line">  <span class="bullet">-</span> <span class="attr">override hydra/hydra_logging:</span> <span class="string">disabled</span> <span class="comment"># 禁用hydra日志, 会覆盖其他配置</span></span><br><span class="line">  <span class="bullet">-</span> <span class="attr">override hydra/job_logging:</span> <span class="string">disabled</span> <span class="comment"># 禁用hydra日志, 会覆盖其他配置</span></span><br><span class="line"></span><br><span class="line"><span class="attr">hydra:</span></span><br><span class="line">  <span class="attr">output_subdir:</span> <span class="literal">null</span> <span class="comment"># 输出目录</span></span><br><span class="line">  <span class="attr">run:</span> <span class="comment"># 运行配置</span></span><br><span class="line">    <span class="attr">dir:</span> <span class="string">.</span> <span class="comment"># 运行目录，相对路径</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 项目配置</span></span><br><span class="line"><span class="attr">project:</span></span><br><span class="line">  <span class="attr">name:</span> <span class="string">Mongolian</span> <span class="string">Sentiment</span> <span class="string">Classification</span> <span class="comment"># 项目名称</span></span><br><span class="line">  <span class="attr">version:</span> <span class="number">0.0</span><span class="number">.1</span> <span class="comment"># 项目版本</span></span><br><span class="line"></span><br><span class="line"><span class="attr">resume:</span> <span class="string">&quot;output/epoch_4_f1_0.2792.pth&quot;</span> <span class="comment"># 恢复训练模型路径</span></span><br><span class="line"><span class="attr">lr:</span> <span class="number">1e-5</span> <span class="comment"># 学习率</span></span><br><span class="line"><span class="attr">train_batch_size:</span> <span class="number">64</span> <span class="comment"># 训练batch size</span></span><br><span class="line"><span class="attr">valid_batch_size:</span> <span class="number">128</span> <span class="comment"># 验证batch size</span></span><br><span class="line"><span class="attr">warm_up_steps:</span> <span class="number">500</span> <span class="comment"># warm up steps</span></span><br><span class="line"><span class="attr">epochs:</span> <span class="number">10</span> <span class="comment"># 训练轮数</span></span><br><span class="line"><span class="attr">device:</span> <span class="string">&quot;cuda:2&quot;</span> <span class="comment"># 训练设备</span></span><br><span class="line"></span><br><span class="line"><span class="attr">pretrained_model_name_or_path:</span> <span class="string">&quot;model&quot;</span> <span class="comment"># 预训练模型路径</span></span><br><span class="line"><span class="attr">output_dir:</span> <span class="string">&quot;output&quot;</span> <span class="comment"># 输出目录</span></span><br><span class="line"><span class="attr">train_file_path:</span> <span class="string">&quot;data/train.csv&quot;</span> <span class="comment"># 训练数据路径</span></span><br><span class="line"><span class="attr">valid_file_path:</span> <span class="string">&quot;data/valid.csv&quot;</span> <span class="comment"># 验证数据路径</span></span><br><span class="line"><span class="attr">weight_decay:</span> <span class="number">0.01</span> <span class="comment"># 权重衰减</span></span><br><span class="line"></span><br><span class="line"><span class="attr">plm_config:</span></span><br><span class="line">  <span class="attr">max_length:</span> <span class="number">256</span> <span class="comment"># 最大序列长度</span></span><br><span class="line">  <span class="attr">num_labels:</span> <span class="number">6</span></span><br><span class="line">  <span class="attr">label2id:</span></span><br><span class="line">    <span class="attr">neural:</span> <span class="number">0</span></span><br><span class="line">    <span class="attr">happy:</span> <span class="number">1</span></span><br><span class="line">    <span class="attr">angry:</span> <span class="number">2</span></span><br><span class="line">    <span class="attr">sad:</span> <span class="number">3</span></span><br><span class="line">    <span class="attr">fear:</span> <span class="number">4</span></span><br><span class="line">    <span class="attr">surprise:</span> <span class="number">5</span></span><br><span class="line">  <span class="attr">id2label:</span></span><br><span class="line">    <span class="attr">0:</span> <span class="string">neural</span></span><br><span class="line">    <span class="attr">1:</span> <span class="string">happy</span></span><br><span class="line">    <span class="attr">2:</span> <span class="string">angry</span></span><br><span class="line">    <span class="attr">3:</span> <span class="string">sad</span></span><br><span class="line">    <span class="attr">4:</span> <span class="string">fear</span></span><br><span class="line">    <span class="attr">5:</span> <span class="string">surprise</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>数据预处理代码：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">split_dataset</span>(<span class="params">data_path: <span class="built_in">str</span> = <span class="string">&quot;data/data.csv&quot;</span>, ratio=<span class="number">0.8</span></span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        划分数据集</span></span><br><span class="line"><span class="string">        :param ratio: 划分比例</span></span><br><span class="line"><span class="string">        :param data_path: 数据集路径</span></span><br><span class="line"><span class="string">        :return:</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="comment"># 读取文件</span></span><br><span class="line">    data = pd.read_csv(data_path, encoding=<span class="string">&#x27;GBK&#x27;</span>, names=[<span class="string">&#x27;text&#x27;</span>, <span class="string">&#x27;label&#x27;</span>])</span><br><span class="line">    data.dropna(axis=<span class="number">0</span>, inplace=<span class="literal">True</span>)  <span class="comment"># 删除空行</span></span><br><span class="line">    data.dropna(axis=<span class="number">1</span>, inplace=<span class="literal">True</span>)  <span class="comment"># 删除空行</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 划分数据集</span></span><br><span class="line">    train_data = data.sample(frac=ratio, random_state=<span class="number">0</span>, axis=<span class="number">0</span>)</span><br><span class="line">    valid_data = data.drop(train_data.index)</span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;数据集大小：<span class="subst">&#123;data.shape[<span class="number">0</span>]&#125;</span>&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;训练集大小：<span class="subst">&#123;train_data.shape[<span class="number">0</span>]&#125;</span>&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;验证集大小：<span class="subst">&#123;valid_data.shape[<span class="number">0</span>]&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 保存数据集</span></span><br><span class="line">    train_data.to_csv(<span class="string">&quot;data/train.csv&quot;</span>, columns=[<span class="string">&quot;text&quot;</span>, <span class="string">&quot;label&quot;</span>], index=<span class="literal">False</span>, encoding=<span class="string">&#x27;GBK&#x27;</span>)</span><br><span class="line">    valid_data.to_csv(<span class="string">&quot;data/valid.csv&quot;</span>, columns=[<span class="string">&quot;text&quot;</span>, <span class="string">&quot;label&quot;</span>], index=<span class="literal">False</span>, encoding=<span class="string">&#x27;GBK&#x27;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
<li><p>加载模型和分词器</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">load_pretrained_model</span>(<span class="params">config</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        加载预训练语言模型</span></span><br><span class="line"><span class="string">        :param config: 配置文件</span></span><br><span class="line"><span class="string">        :return: 模型和分词器</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="comment"># 加载配置文件和修改配置文件</span></span><br><span class="line">    plm_config = XLMRobertaConfig.from_pretrained(config[<span class="string">&#x27;pretrained_model_name_or_path&#x27;</span>])</span><br><span class="line">    <span class="keyword">for</span> key, value <span class="keyword">in</span> config[<span class="string">&#x27;plm_config&#x27;</span>].items():</span><br><span class="line">        <span class="built_in">setattr</span>(plm_config, key, value)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 加载预训练语言模型和分词器</span></span><br><span class="line">    model = XLMRobertaForSequenceClassification.from_pretrained(config[<span class="string">&#x27;pretrained_model_name_or_path&#x27;</span>])</span><br><span class="line">    tokenizer = XLMRobertaTokenizerFast.from_pretrained(config[<span class="string">&#x27;pretrained_model_name_or_path&#x27;</span>])</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 修改模型的分类器配置文件</span></span><br><span class="line">    model.plm_config = plm_config</span><br><span class="line">    model.num_labels = plm_config.num_labels</span><br><span class="line">    model.classifier = RobertaClassificationHead(plm_config)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 冻结模型</span></span><br><span class="line">    freeze_model(model.roberta)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 返回模型和分词器</span></span><br><span class="line">    <span class="keyword">return</span> model, tokenizer</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
<li><p>模型训练</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">do_train</span>(<span class="params">config</span>):</span><br><span class="line">    epochs, device = config[<span class="string">&#x27;epochs&#x27;</span>], torch.device(config[<span class="string">&#x27;device&#x27;</span>])</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 加载数据集</span></span><br><span class="line">    train_loader, valid_loader = load_data(config)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 计算总步数</span></span><br><span class="line">    total_steps = <span class="built_in">len</span>(train_loader) * epochs</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 加载模型</span></span><br><span class="line">    model, tokenizer = load_pretrained_model(config)</span><br><span class="line">    model.to(device)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 加载优化器</span></span><br><span class="line">    optimizer = AdamW(model.parameters(), lr=config[<span class="string">&#x27;lr&#x27;</span>], weight_decay=config[<span class="string">&#x27;weight_decay&#x27;</span>])</span><br><span class="line">    scheduler = get_linear_schedule_with_warmup(</span><br><span class="line">        optimizer=optimizer,</span><br><span class="line">        num_warmup_steps=config[<span class="string">&#x27;warm_up_steps&#x27;</span>],</span><br><span class="line">        num_training_steps=total_steps</span><br><span class="line">    )</span><br><span class="line"></span><br><span class="line">    best_ckpt_path, best_f1 = <span class="string">&quot;&quot;</span>, <span class="number">0</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 断续训练</span></span><br><span class="line">    <span class="keyword">if</span> config[<span class="string">&#x27;resume&#x27;</span>]:</span><br><span class="line">        <span class="keyword">assert</span> os.path.exists(config[<span class="string">&#x27;resume&#x27;</span>]), <span class="string">f&quot;文件不存在：<span class="subst">&#123;config[<span class="string">&#x27;resume&#x27;</span>]&#125;</span>&quot;</span></span><br><span class="line">        resume_result = torch.load(config[<span class="string">&#x27;resume&#x27;</span>])</span><br><span class="line">        model.load_state_dict(resume_result[<span class="string">&#x27;model&#x27;</span>])</span><br><span class="line">        optimizer.load_state_dict(resume_result[<span class="string">&#x27;optimizer&#x27;</span>])</span><br><span class="line">        scheduler.load_state_dict(resume_result[<span class="string">&#x27;scheduler&#x27;</span>])</span><br><span class="line">        best_ckpt_path = config[<span class="string">&#x27;resume&#x27;</span>]</span><br><span class="line">        best_f1 = resume_result[<span class="string">&#x27;metric_dict&#x27;</span>][<span class="string">&#x27;f1&#x27;</span>]</span><br><span class="line"></span><br><span class="line">    pbar = tqdm(dynamic_ncols=<span class="literal">True</span>, total=total_steps)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> epoch <span class="keyword">in</span> <span class="built_in">range</span>(epochs):</span><br><span class="line">        <span class="comment"># model 设置为训练模式</span></span><br><span class="line">        model.train()</span><br><span class="line">        pbar.set_description(<span class="string">f&#x27;[<span class="subst">&#123;epoch + <span class="number">1</span>&#125;</span>/<span class="subst">&#123;epochs&#125;</span>]&#x27;</span>)</span><br><span class="line">        <span class="keyword">for</span> item <span class="keyword">in</span> train_loader:</span><br><span class="line">            texts, labels = item[<span class="string">&#x27;text&#x27;</span>], item[<span class="string">&#x27;label&#x27;</span>]</span><br><span class="line">            model_inputs = tokenizer(</span><br><span class="line">                texts,</span><br><span class="line">                padding=<span class="literal">True</span>,</span><br><span class="line">                truncation=<span class="literal">True</span>,</span><br><span class="line">                max_length=config[<span class="string">&#x27;plm_config&#x27;</span>][<span class="string">&#x27;max_length&#x27;</span>],</span><br><span class="line">                return_tensors=<span class="string">&quot;pt&quot;</span></span><br><span class="line">            )</span><br><span class="line"></span><br><span class="line">            <span class="keyword">for</span> key <span class="keyword">in</span> model_inputs.keys():</span><br><span class="line">                model_inputs[key] = model_inputs[key].to(device)</span><br><span class="line"></span><br><span class="line">            labels = torch.LongTensor(labels).to(device) - <span class="number">1</span></span><br><span class="line">            outputs = model(**model_inputs, labels=labels)</span><br><span class="line">            loss = outputs.loss</span><br><span class="line"></span><br><span class="line">            pbar.set_postfix(&#123;<span class="string">&#x27;loss&#x27;</span>: <span class="built_in">round</span>(loss.item(), <span class="number">4</span>)&#125;)</span><br><span class="line">            pbar.update(<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">            optimizer.zero_grad()</span><br><span class="line">            loss.backward()</span><br><span class="line">            optimizer.step()</span><br><span class="line">            scheduler.step()</span><br><span class="line"></span><br><span class="line">        metric_dict = do_evaluate(</span><br><span class="line">            model,</span><br><span class="line">            tokenizer,</span><br><span class="line">            valid_loader,</span><br><span class="line">            config[<span class="string">&#x27;plm_config&#x27;</span>][<span class="string">&#x27;max_length&#x27;</span>],</span><br><span class="line">            device</span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">        pp(metric_dict)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> metric_dict[<span class="string">&#x27;f1&#x27;</span>] &gt; best_f1:</span><br><span class="line">            best_f1 = metric_dict[<span class="string">&#x27;f1&#x27;</span>]</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> os.path.exists(best_ckpt_path):</span><br><span class="line">                os.remove(best_ckpt_path)</span><br><span class="line"></span><br><span class="line">            best_ckpt_path = <span class="string">f&quot;<span class="subst">&#123;config[<span class="string">&#x27;output_dir&#x27;</span>]&#125;</span>/epoch_<span class="subst">&#123;epoch + <span class="number">1</span>&#125;</span>_f1_<span class="subst">&#123;<span class="built_in">round</span>(best_f1, <span class="number">4</span>)&#125;</span>.pth&quot;</span></span><br><span class="line"></span><br><span class="line">            torch.save(&#123;</span><br><span class="line">                <span class="string">&#x27;model&#x27;</span>: model.state_dict(),</span><br><span class="line">                <span class="string">&#x27;optimizer&#x27;</span>: optimizer.state_dict(),</span><br><span class="line">                <span class="string">&#x27;scheduler&#x27;</span>: scheduler.state_dict(),</span><br><span class="line">                <span class="string">&#x27;metric_dict&#x27;</span>: metric_dict</span><br><span class="line">            &#125;, best_ckpt_path)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 保存最佳模型的路径</span></span><br><span class="line">    <span class="keyword">return</span> best_ckpt_path</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
<li><p>模型验证</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@torch.no_grad()</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">do_evaluate</span>(<span class="params">model, tokenizer, valid_loader, max_length, device</span>) -&gt; <span class="built_in">dict</span>:</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        评估模型</span></span><br><span class="line"><span class="string">        :param model: 模型</span></span><br><span class="line"><span class="string">        :param tokenizer: 分词器</span></span><br><span class="line"><span class="string">        :param valid_loader: 验证集</span></span><br><span class="line"><span class="string">        :param max_length: 最大长度</span></span><br><span class="line"><span class="string">        :param device: 设备</span></span><br><span class="line"><span class="string">        :return: 评估结果</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="comment"># model 设置为评估模式</span></span><br><span class="line">    model.<span class="built_in">eval</span>()</span><br><span class="line">    total_predict, total_ground = [], []</span><br><span class="line">    <span class="keyword">for</span> item <span class="keyword">in</span> tqdm(valid_loader, dynamic_ncols=<span class="literal">True</span>, desc=<span class="string">&#x27;evaluating...&#x27;</span>):</span><br><span class="line">        texts, labels = item[<span class="string">&#x27;text&#x27;</span>], item[<span class="string">&#x27;label&#x27;</span>]</span><br><span class="line">        model_inputs = tokenizer(</span><br><span class="line">            texts,</span><br><span class="line">            padding=<span class="literal">True</span>,</span><br><span class="line">            truncation=<span class="literal">True</span>,</span><br><span class="line">            max_length=max_length,</span><br><span class="line">            return_tensors=<span class="string">&quot;pt&quot;</span></span><br><span class="line">        )</span><br><span class="line">        <span class="keyword">for</span> key <span class="keyword">in</span> model_inputs.keys():</span><br><span class="line">            model_inputs[key] = model_inputs[key].to(device)</span><br><span class="line"></span><br><span class="line">        labels = torch.LongTensor(labels).to(device) - <span class="number">1</span></span><br><span class="line">        outputs = model(**model_inputs, labels=labels)</span><br><span class="line">        logits = outputs.logits</span><br><span class="line">        predict = logits.argmax(dim=-<span class="number">1</span>).cpu().numpy().tolist()</span><br><span class="line">        total_predict.extend(predict)</span><br><span class="line">        total_ground.extend(labels.cpu().numpy().tolist())</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        <span class="string">&quot;f1&quot;</span>: f1_score(total_ground, total_predict, average=<span class="string">&#x27;macro&#x27;</span>),</span><br><span class="line">        <span class="string">&quot;acc&quot;</span>: accuracy_score(total_ground, total_predict),</span><br><span class="line">        <span class="string">&quot;recall&quot;</span>: recall_score(total_ground, total_predict, average=<span class="string">&#x27;macro&#x27;</span>),</span><br><span class="line">        <span class="string">&quot;precision&quot;</span>: precision_score(total_ground, total_predict, average=<span class="string">&#x27;macro&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="完整训练代码如下："><a href="#完整训练代码如下：" class="headerlink" title="完整训练代码如下："></a>完整训练代码如下：</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*-coding: Utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Project Name: d2l-zh</span></span><br><span class="line"><span class="comment"># @File: __init__.py.py</span></span><br><span class="line"><span class="comment"># @Author: David</span></span><br><span class="line"><span class="comment"># @Date：2023/8/31 23:13</span></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">from</span> pprint <span class="keyword">import</span> pp</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> hydra</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> torch</span><br><span class="line"><span class="keyword">from</span> omegaconf <span class="keyword">import</span> OmegaConf</span><br><span class="line"><span class="keyword">from</span> sklearn.metrics <span class="keyword">import</span> f1_score, precision_score, accuracy_score, recall_score</span><br><span class="line"><span class="keyword">from</span> torch <span class="keyword">import</span> nn</span><br><span class="line"><span class="keyword">from</span> torch.optim.adamw <span class="keyword">import</span> AdamW</span><br><span class="line"><span class="keyword">from</span> torch.utils.data <span class="keyword">import</span> DataLoader, Dataset</span><br><span class="line"><span class="keyword">from</span> tqdm <span class="keyword">import</span> tqdm</span><br><span class="line"><span class="keyword">from</span> transformers <span class="keyword">import</span> XLMRobertaForSequenceClassification, XLMRobertaConfig, XLMRobertaTokenizerFast, get_linear_schedule_with_warmup</span><br><span class="line"><span class="keyword">from</span> transformers.models.roberta.modeling_roberta <span class="keyword">import</span> RobertaClassificationHead</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">split_dataset</span>(<span class="params">data_path: <span class="built_in">str</span> = <span class="string">&quot;data/data.csv&quot;</span>, ratio=<span class="number">0.8</span></span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        划分数据集</span></span><br><span class="line"><span class="string">        :param ratio: 划分比例</span></span><br><span class="line"><span class="string">        :param data_path: 数据集路径</span></span><br><span class="line"><span class="string">        :return:</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="comment"># 读取文件</span></span><br><span class="line">    data = pd.read_csv(data_path, encoding=<span class="string">&#x27;GBK&#x27;</span>, names=[<span class="string">&#x27;text&#x27;</span>, <span class="string">&#x27;label&#x27;</span>])</span><br><span class="line">    data.dropna(axis=<span class="number">0</span>, inplace=<span class="literal">True</span>)  <span class="comment"># 删除空行</span></span><br><span class="line">    data.dropna(axis=<span class="number">1</span>, inplace=<span class="literal">True</span>)  <span class="comment"># 删除空行</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 划分数据集</span></span><br><span class="line">    train_data = data.sample(frac=ratio, random_state=<span class="number">0</span>, axis=<span class="number">0</span>)</span><br><span class="line">    valid_data = data.drop(train_data.index)</span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;数据集大小：<span class="subst">&#123;data.shape[<span class="number">0</span>]&#125;</span>&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;训练集大小：<span class="subst">&#123;train_data.shape[<span class="number">0</span>]&#125;</span>&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;验证集大小：<span class="subst">&#123;valid_data.shape[<span class="number">0</span>]&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 保存数据集</span></span><br><span class="line">    train_data.to_csv(<span class="string">&quot;data/train.csv&quot;</span>, columns=[<span class="string">&quot;text&quot;</span>, <span class="string">&quot;label&quot;</span>], index=<span class="literal">False</span>, encoding=<span class="string">&#x27;GBK&#x27;</span>)</span><br><span class="line">    valid_data.to_csv(<span class="string">&quot;data/valid.csv&quot;</span>, columns=[<span class="string">&quot;text&quot;</span>, <span class="string">&quot;label&quot;</span>], index=<span class="literal">False</span>, encoding=<span class="string">&#x27;GBK&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">freeze_model</span>(<span class="params">model: nn.Module</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        冻结模型</span></span><br><span class="line"><span class="string">        :param model: 模型</span></span><br><span class="line"><span class="string">        :return:</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="keyword">for</span> p <span class="keyword">in</span> model.parameters():</span><br><span class="line">        p.requires_grad = <span class="literal">False</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">load_pretrained_model</span>(<span class="params">config</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        加载预训练语言模型</span></span><br><span class="line"><span class="string">        :param config: 配置文件</span></span><br><span class="line"><span class="string">        :return: 模型和分词器</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="comment"># 加载配置文件和修改配置文件</span></span><br><span class="line">    plm_config = XLMRobertaConfig.from_pretrained(config[<span class="string">&#x27;pretrained_model_name_or_path&#x27;</span>])</span><br><span class="line">    <span class="keyword">for</span> key, value <span class="keyword">in</span> config[<span class="string">&#x27;plm_config&#x27;</span>].items():</span><br><span class="line">        <span class="built_in">setattr</span>(plm_config, key, value)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 加载预训练语言模型和分词器</span></span><br><span class="line">    model = XLMRobertaForSequenceClassification.from_pretrained(config[<span class="string">&#x27;pretrained_model_name_or_path&#x27;</span>])</span><br><span class="line">    tokenizer = XLMRobertaTokenizerFast.from_pretrained(config[<span class="string">&#x27;pretrained_model_name_or_path&#x27;</span>])</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 修改模型的分类器配置文件</span></span><br><span class="line">    model.plm_config = plm_config</span><br><span class="line">    model.num_labels = plm_config.num_labels</span><br><span class="line">    model.classifier = RobertaClassificationHead(plm_config)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 冻结模型</span></span><br><span class="line">    freeze_model(model.roberta)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 返回模型和分词器</span></span><br><span class="line">    <span class="keyword">return</span> model, tokenizer</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyDataset</span>(<span class="title class_ inherited__">Dataset</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, csv_file_path</span>):</span><br><span class="line">        <span class="built_in">super</span>(MyDataset, <span class="variable language_">self</span>).__init__()</span><br><span class="line">        df = pd.read_csv(csv_file_path, encoding=<span class="string">&quot;GBK&quot;</span>)</span><br><span class="line">        <span class="variable language_">self</span>.texts = df[<span class="string">&#x27;text&#x27;</span>].values.tolist()</span><br><span class="line">        <span class="variable language_">self</span>.labels = df[<span class="string">&#x27;label&#x27;</span>].values.tolist()</span><br><span class="line">        <span class="variable language_">self</span>.size = df.shape[<span class="number">0</span>]</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__getitem__</span>(<span class="params">self, idx</span>):</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            <span class="string">&#x27;text&#x27;</span>: <span class="variable language_">self</span>.texts[idx],</span><br><span class="line">            <span class="string">&#x27;label&#x27;</span>: <span class="variable language_">self</span>.labels[idx]</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__len__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">self</span>.size</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">load_data</span>(<span class="params">config</span>):</span><br><span class="line">    train_path, valid_path = config[<span class="string">&#x27;train_file_path&#x27;</span>], config[<span class="string">&#x27;valid_file_path&#x27;</span>]</span><br><span class="line">    train_dataset = MyDataset(train_path)</span><br><span class="line">    valid_dataset = MyDataset(valid_path)</span><br><span class="line">    train_loader = DataLoader(</span><br><span class="line">        dataset=train_dataset,</span><br><span class="line">        shuffle=<span class="literal">True</span>,</span><br><span class="line">        drop_last=<span class="literal">False</span>,</span><br><span class="line">        batch_size=config[<span class="string">&#x27;train_batch_size&#x27;</span>]</span><br><span class="line">    )</span><br><span class="line">    valid_loader = DataLoader(</span><br><span class="line">        dataset=valid_dataset,</span><br><span class="line">        shuffle=<span class="literal">False</span>,</span><br><span class="line">        drop_last=<span class="literal">False</span>,</span><br><span class="line">        batch_size=config[<span class="string">&#x27;valid_batch_size&#x27;</span>]</span><br><span class="line">    )</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> train_loader, valid_loader</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">do_train</span>(<span class="params">config</span>):</span><br><span class="line">    epochs, device = config[<span class="string">&#x27;epochs&#x27;</span>], torch.device(config[<span class="string">&#x27;device&#x27;</span>])</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 加载数据集</span></span><br><span class="line">    train_loader, valid_loader = load_data(config)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 计算总步数</span></span><br><span class="line">    total_steps = <span class="built_in">len</span>(train_loader) * epochs</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 加载模型</span></span><br><span class="line">    model, tokenizer = load_pretrained_model(config)</span><br><span class="line">    model.to(device)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 加载优化器</span></span><br><span class="line">    optimizer = AdamW(model.parameters(), lr=config[<span class="string">&#x27;lr&#x27;</span>], weight_decay=config[<span class="string">&#x27;weight_decay&#x27;</span>])</span><br><span class="line">    scheduler = get_linear_schedule_with_warmup(</span><br><span class="line">        optimizer=optimizer,</span><br><span class="line">        num_warmup_steps=config[<span class="string">&#x27;warm_up_steps&#x27;</span>],</span><br><span class="line">        num_training_steps=total_steps</span><br><span class="line">    )</span><br><span class="line"></span><br><span class="line">    best_ckpt_path, best_f1 = <span class="string">&quot;&quot;</span>, <span class="number">0</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 断续训练</span></span><br><span class="line">    <span class="keyword">if</span> config[<span class="string">&#x27;resume&#x27;</span>]:</span><br><span class="line">        <span class="keyword">assert</span> os.path.exists(config[<span class="string">&#x27;resume&#x27;</span>]), <span class="string">f&quot;文件不存在：<span class="subst">&#123;config[<span class="string">&#x27;resume&#x27;</span>]&#125;</span>&quot;</span></span><br><span class="line">        resume_result = torch.load(config[<span class="string">&#x27;resume&#x27;</span>])</span><br><span class="line">        model.load_state_dict(resume_result[<span class="string">&#x27;model&#x27;</span>])</span><br><span class="line">        optimizer.load_state_dict(resume_result[<span class="string">&#x27;optimizer&#x27;</span>])</span><br><span class="line">        scheduler.load_state_dict(resume_result[<span class="string">&#x27;scheduler&#x27;</span>])</span><br><span class="line">        best_ckpt_path = config[<span class="string">&#x27;resume&#x27;</span>]</span><br><span class="line">        best_f1 = resume_result[<span class="string">&#x27;metric_dict&#x27;</span>][<span class="string">&#x27;f1&#x27;</span>]</span><br><span class="line"></span><br><span class="line">    pbar = tqdm(dynamic_ncols=<span class="literal">True</span>, total=total_steps)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> epoch <span class="keyword">in</span> <span class="built_in">range</span>(epochs):</span><br><span class="line">        <span class="comment"># model 设置为训练模式</span></span><br><span class="line">        model.train()</span><br><span class="line">        pbar.set_description(<span class="string">f&#x27;[<span class="subst">&#123;epoch + <span class="number">1</span>&#125;</span>/<span class="subst">&#123;epochs&#125;</span>]&#x27;</span>)</span><br><span class="line">        <span class="keyword">for</span> item <span class="keyword">in</span> train_loader:</span><br><span class="line">            texts, labels = item[<span class="string">&#x27;text&#x27;</span>], item[<span class="string">&#x27;label&#x27;</span>]</span><br><span class="line">            model_inputs = tokenizer(</span><br><span class="line">                texts,</span><br><span class="line">                padding=<span class="literal">True</span>,</span><br><span class="line">                truncation=<span class="literal">True</span>,</span><br><span class="line">                max_length=config[<span class="string">&#x27;plm_config&#x27;</span>][<span class="string">&#x27;max_length&#x27;</span>],</span><br><span class="line">                return_tensors=<span class="string">&quot;pt&quot;</span></span><br><span class="line">            )</span><br><span class="line"></span><br><span class="line">            <span class="keyword">for</span> key <span class="keyword">in</span> model_inputs.keys():</span><br><span class="line">                model_inputs[key] = model_inputs[key].to(device)</span><br><span class="line"></span><br><span class="line">            labels = torch.LongTensor(labels).to(device) - <span class="number">1</span></span><br><span class="line">            outputs = model(**model_inputs, labels=labels)</span><br><span class="line">            loss = outputs.loss</span><br><span class="line"></span><br><span class="line">            pbar.set_postfix(&#123;<span class="string">&#x27;loss&#x27;</span>: <span class="built_in">round</span>(loss.item(), <span class="number">4</span>)&#125;)</span><br><span class="line">            pbar.update(<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">            optimizer.zero_grad()</span><br><span class="line">            loss.backward()</span><br><span class="line">            optimizer.step()</span><br><span class="line">            scheduler.step()</span><br><span class="line"></span><br><span class="line">        metric_dict = do_evaluate(</span><br><span class="line">            model,</span><br><span class="line">            tokenizer,</span><br><span class="line">            valid_loader,</span><br><span class="line">            config[<span class="string">&#x27;plm_config&#x27;</span>][<span class="string">&#x27;max_length&#x27;</span>],</span><br><span class="line">            device</span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">        pp(metric_dict)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> metric_dict[<span class="string">&#x27;f1&#x27;</span>] &gt; best_f1:</span><br><span class="line">            best_f1 = metric_dict[<span class="string">&#x27;f1&#x27;</span>]</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> os.path.exists(best_ckpt_path):</span><br><span class="line">                os.remove(best_ckpt_path)</span><br><span class="line"></span><br><span class="line">            best_ckpt_path = <span class="string">f&quot;<span class="subst">&#123;config[<span class="string">&#x27;output_dir&#x27;</span>]&#125;</span>/epoch_<span class="subst">&#123;epoch + <span class="number">1</span>&#125;</span>_f1_<span class="subst">&#123;<span class="built_in">round</span>(best_f1, <span class="number">4</span>)&#125;</span>.pth&quot;</span></span><br><span class="line"></span><br><span class="line">            torch.save(&#123;</span><br><span class="line">                <span class="string">&#x27;model&#x27;</span>: model.state_dict(),</span><br><span class="line">                <span class="string">&#x27;optimizer&#x27;</span>: optimizer.state_dict(),</span><br><span class="line">                <span class="string">&#x27;scheduler&#x27;</span>: scheduler.state_dict(),</span><br><span class="line">                <span class="string">&#x27;metric_dict&#x27;</span>: metric_dict</span><br><span class="line">            &#125;, best_ckpt_path)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 保存最佳模型的路径</span></span><br><span class="line">    <span class="keyword">return</span> best_ckpt_path</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@torch.no_grad()</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">do_evaluate</span>(<span class="params">model, tokenizer, valid_loader, max_length, device</span>) -&gt; <span class="built_in">dict</span>:</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        评估模型</span></span><br><span class="line"><span class="string">        :param model: 模型</span></span><br><span class="line"><span class="string">        :param tokenizer: 分词器</span></span><br><span class="line"><span class="string">        :param valid_loader: 验证集</span></span><br><span class="line"><span class="string">        :param max_length: 最大长度</span></span><br><span class="line"><span class="string">        :param device: 设备</span></span><br><span class="line"><span class="string">        :return: 评估结果</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="comment"># model 设置为评估模式</span></span><br><span class="line">    model.<span class="built_in">eval</span>()</span><br><span class="line">    total_predict, total_ground = [], []</span><br><span class="line">    <span class="keyword">for</span> item <span class="keyword">in</span> tqdm(valid_loader, dynamic_ncols=<span class="literal">True</span>, desc=<span class="string">&#x27;evaluating...&#x27;</span>):</span><br><span class="line">        texts, labels = item[<span class="string">&#x27;text&#x27;</span>], item[<span class="string">&#x27;label&#x27;</span>]</span><br><span class="line">        model_inputs = tokenizer(</span><br><span class="line">            texts,</span><br><span class="line">            padding=<span class="literal">True</span>,</span><br><span class="line">            truncation=<span class="literal">True</span>,</span><br><span class="line">            max_length=max_length,</span><br><span class="line">            return_tensors=<span class="string">&quot;pt&quot;</span></span><br><span class="line">        )</span><br><span class="line">        <span class="keyword">for</span> key <span class="keyword">in</span> model_inputs.keys():</span><br><span class="line">            model_inputs[key] = model_inputs[key].to(device)</span><br><span class="line"></span><br><span class="line">        labels = torch.LongTensor(labels).to(device) - <span class="number">1</span></span><br><span class="line">        outputs = model(**model_inputs, labels=labels)</span><br><span class="line">        logits = outputs.logits</span><br><span class="line">        predict = logits.argmax(dim=-<span class="number">1</span>).cpu().numpy().tolist()</span><br><span class="line">        total_predict.extend(predict)</span><br><span class="line">        total_ground.extend(labels.cpu().numpy().tolist())</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        <span class="string">&quot;f1&quot;</span>: f1_score(total_ground, total_predict, average=<span class="string">&#x27;macro&#x27;</span>),</span><br><span class="line">        <span class="string">&quot;acc&quot;</span>: accuracy_score(total_ground, total_predict),</span><br><span class="line">        <span class="string">&quot;recall&quot;</span>: recall_score(total_ground, total_predict, average=<span class="string">&#x27;macro&#x27;</span>),</span><br><span class="line">        <span class="string">&quot;precision&quot;</span>: precision_score(total_ground, total_predict, average=<span class="string">&#x27;macro&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">check</span>():</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        检查数据集</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    data = pd.read_csv(<span class="string">&quot;data/valid.csv&quot;</span>, encoding=<span class="string">&#x27;GBK&#x27;</span>, names=[<span class="string">&#x27;text&#x27;</span>, <span class="string">&#x27;label&#x27;</span>])</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 按行遍历 data</span></span><br><span class="line">    <span class="keyword">for</span> i, row <span class="keyword">in</span> data.iterrows():</span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">isinstance</span>(row[<span class="string">&#x27;text&#x27;</span>], <span class="built_in">float</span>) <span class="keyword">or</span> <span class="built_in">isinstance</span>(row[<span class="string">&#x27;label&#x27;</span>], <span class="built_in">float</span>):</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">f&quot;text or label is empty: <span class="subst">&#123;i&#125;</span>&quot;</span>)</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">f&quot;text <span class="subst">&#123;row[<span class="string">&#x27;text&#x27;</span>]&#125;</span>, label <span class="subst">&#123;row[<span class="string">&#x27;label&#x27;</span>]&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@hydra.main(<span class="params"></span></span></span><br><span class="line"><span class="params"><span class="meta">    config_path=<span class="string">&#x27;config&#x27;</span>,  <span class="comment"># 配置文件路径</span></span></span></span><br><span class="line"><span class="params"><span class="meta">    config_name=<span class="string">&#x27;config&#x27;</span>,  <span class="comment"># 配置文件名称</span></span></span></span><br><span class="line"><span class="params"><span class="meta">    version_base=<span class="string">&#x27;1.3.2&#x27;</span>  <span class="comment"># 版本号</span></span></span></span><br><span class="line"><span class="params"><span class="meta"></span>)</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">main</span>(<span class="params">config</span>):</span><br><span class="line">    config = OmegaConf.to_container(config, resolve=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 创建输出目录</span></span><br><span class="line">    os.makedirs(config[<span class="string">&#x27;output_dir&#x27;</span>], exist_ok=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># split_dataset() # 划分数据集</span></span><br><span class="line">    do_train(config)  <span class="comment"># 训练模型</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    split_dataset()</span><br><span class="line">    check()</span><br><span class="line">    main()</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="2-发现新的软件使用技巧"><a href="#2-发现新的软件使用技巧" class="headerlink" title="2. 发现新的软件使用技巧"></a>2. 发现新的软件使用技巧</h2><p><code>Typora</code> 软件可以打开控制台，本质上和浏览器没有区别，只是一个套壳的浏览器而已</p>
<p><img src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blogimage-20230901014434093.png" alt="image-20230901014434093"></p>
<p><img src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blogimage-20230901014452319.png" alt="image-20230901014452319"></p>
<h2 id="3-错误解决"><a href="#3-错误解决" class="headerlink" title="3. 错误解决"></a>3. 错误解决</h2><p>错误提示：<code>TypeError: TextEncodeInput must be Union[TextInputSequence,Tupele[InputSequence, InputSequence]]</code></p>
<p>问题原因：数据集中存在空值或者 <code>nan</code></p>
<p>解决方案：处理数据时，删除数据中的空值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">split_dataset</span>(<span class="params">data_path: <span class="built_in">str</span> = <span class="string">&quot;data/data.csv&quot;</span>, ratio=<span class="number">0.8</span></span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        划分数据集</span></span><br><span class="line"><span class="string">        :param ratio: 划分比例</span></span><br><span class="line"><span class="string">        :param data_path: 数据集路径</span></span><br><span class="line"><span class="string">        :return:</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="comment"># 读取文件</span></span><br><span class="line">    data = pd.read_csv(data_path, encoding=<span class="string">&#x27;GBK&#x27;</span>, names=[<span class="string">&#x27;text&#x27;</span>, <span class="string">&#x27;label&#x27;</span>])</span><br><span class="line"></span><br><span class="line">    <span class="comment"># here</span></span><br><span class="line">    data.dropna(axis=<span class="number">0</span>, inplace=<span class="literal">True</span>)  <span class="comment"># 删除空行</span></span><br><span class="line">    data.dropna(axis=<span class="number">1</span>, inplace=<span class="literal">True</span>)  <span class="comment"># 删除空行</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 划分数据集</span></span><br><span class="line">    train_data = data.sample(frac=ratio, random_state=<span class="number">0</span>, axis=<span class="number">0</span>)</span><br><span class="line">    valid_data = data.drop(train_data.index)</span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;数据集大小：<span class="subst">&#123;data.shape[<span class="number">0</span>]&#125;</span>&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;训练集大小：<span class="subst">&#123;train_data.shape[<span class="number">0</span>]&#125;</span>&quot;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;验证集大小：<span class="subst">&#123;valid_data.shape[<span class="number">0</span>]&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 保存数据集</span></span><br><span class="line">    train_data.to_csv(<span class="string">&quot;data/train.csv&quot;</span>, columns=[<span class="string">&quot;text&quot;</span>, <span class="string">&quot;label&quot;</span>], index=<span class="literal">False</span>, encoding=<span class="string">&#x27;GBK&#x27;</span>)</span><br><span class="line">    valid_data.to_csv(<span class="string">&quot;data/valid.csv&quot;</span>, columns=[<span class="string">&quot;text&quot;</span>, <span class="string">&quot;label&quot;</span>], index=<span class="literal">False</span>, encoding=<span class="string">&#x27;GBK&#x27;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://blog.david-deng.cn">David</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://blog.david-deng.cn/2023/08/31/2023-08-31%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">https://blog.david-deng.cn/2023/08/31/2023-08-31 学习笔记/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow noreferrer" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来源 <a href="https://blog.david-deng.cn" target="_blank">David 的博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E7%AC%94%E8%AE%B0/">笔记</a><a class="post-meta__tags" href="/tags/Pytorch/">Pytorch</a></div><div class="post-share"><div class="social-share" data-image="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-7.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i>赞助</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/WeChatPay.jpg" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/WeChatPay.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/Alipay.jpg" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/Alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><a class="pagination-related" href="/2023/08/30/%E7%A0%94%E4%BA%8C%E5%AD%A6%E5%B9%B4%E8%AE%A1%E5%88%92%20&amp;%20%E5%AE%89%E6%8E%92/" title="研二学年计划 &amp; 安排"><img class="cover" src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-6.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="info"><div class="info-1"><div class="info-item-1">上一篇</div><div class="info-item-2">研二学年计划 & 安排</div></div><div class="info-2"><div class="info-item-1">研二学年计划 &amp; 安排研二学年计划 —— 想干啥？  通过大学英语六级  发文章  写博客  入党  研二学年安排 —— 怎么干？通过大学英语六级 背单词，坚持每天 背单词 ！！！ 学语法，学习基础的英语语法 练习听力，阅读，写作，主要是阅读的速度和写作的能力  发文章目前正在做的一些工作如下：  吐槽大会数据集 讽刺生成（强化学习） 文本风格转换综述 中药 VQA（数据集 + 模型）  目前各个项目的进展如下（截止到 2023.08.30）：  吐槽大会数据集正在进行字幕校对的工作，后续工作主要是完成字幕校对，进行情感标注，撰写文章和文章修改 讽刺生成目前正在可行性验证阶段，后续的工作主要是完成模型的构建，撰写文章和文章修改 文本风格转换数据集正在进行论文收集和阅读的工作，后续的工作主要是进行研究方向的工作总结，撰写文章和文章修改 中药 VQA 数据集已经基本构建完成，目前正在进行的是基线模型的复现，后续工作主要是将基线模型使用在我们的数据集中，然后构建自己的用于 VQA...</div></div></div></a><a class="pagination-related" href="/2023/10/11/other-%E9%9A%8F%E7%AC%94-%E9%97%AE%E9%A2%98%E7%AC%94%E8%AE%B0/" title="问题笔记"><img class="cover" src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="info text-right"><div class="info-1"><div class="info-item-1">下一篇</div><div class="info-item-2">问题笔记</div></div><div class="info-2"><div class="info-item-1">遇到的问题&amp;解决方案【持续更新~】2023-09-16 笔记解决 MP4 视频转成 m3u8 格式的问题 需求描述： 现在需要批量将一些存放在文件夹下的 MP4 视频转成 m3u8 格式，其中视频存放的位置可能是多层文件夹下，m3u8 视频需要分门别类的存放  解决思路  使用 Python + ffmpeg 实现视频的转码 使用多线程加速视频的转码   遇到的问题  多个视频不能按照预设的顺序进行转码，前一个视频还没有完成，下一个视频就中断了前一个视频的转码 本机的性能不足，需要放在服务器上运行，但是服务器上的Linux环境导致运行出现一些没有预测到的问题   最终解决方案  不使用多线程运行 使用Python...</div></div></div></a></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><a class="pagination-related" href="/2023/05/05/Deep-Learning-Code-Note/" title="深度学习代码笔记-01"><img class="cover" src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-4.png" alt="cover"><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-05</div><div class="info-item-2">深度学习代码笔记-01</div></div><div class="info-2"><div class="info-item-1">深度学习代码笔记-011. 配置环境1.1 Conda 任选其一（推荐后者）  Anaconda 安装 Miniconda 安装   1.2 Conda 常用命令123456789101112131415161718192021222324252627282930# 显示所有环境conda env list# 显示当前环境下的包conda list# 创建conda环境conda create -n 环境名 python=版本号# 删除conda环境conda remove -n 环境名 --all# 进入conda环境conda activate 环境名# 退出conda环境conda deactivate# 删除缓存conda clean -a -y# conda环境导出conda activate 环境名conda env export &gt; env.yaml# conda环境迁移conda env create -f env.yaml# conda国内源conda config --add channels...</div></div></div></a><a class="pagination-related" href="/2023/10/11/other-%E9%9A%8F%E7%AC%94-%E9%97%AE%E9%A2%98%E7%AC%94%E8%AE%B0/" title="问题笔记"><img class="cover" src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png" alt="cover"><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2023-10-11</div><div class="info-item-2">问题笔记</div></div><div class="info-2"><div class="info-item-1">遇到的问题&amp;解决方案【持续更新~】2023-09-16 笔记解决 MP4 视频转成 m3u8 格式的问题 需求描述： 现在需要批量将一些存放在文件夹下的 MP4 视频转成 m3u8 格式，其中视频存放的位置可能是多层文件夹下，m3u8 视频需要分门别类的存放  解决思路  使用 Python + ffmpeg 实现视频的转码 使用多线程加速视频的转码   遇到的问题  多个视频不能按照预设的顺序进行转码，前一个视频还没有完成，下一个视频就中断了前一个视频的转码 本机的性能不足，需要放在服务器上运行，但是服务器上的Linux环境导致运行出现一些没有预测到的问题   最终解决方案  不使用多线程运行 使用Python...</div></div></div></a><a class="pagination-related" href="/2025/01/05/other-%E9%9A%8F%E7%AC%94-icarus-%E4%B8%BB%E9%A2%98%E5%AE%89%E8%A3%85/" title="Hexo 配置 Icarus 主题"><img class="cover" src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-4.png" alt="cover"><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-01-05</div><div class="info-item-2">Hexo 配置 Icarus 主题</div></div><div class="info-2"><div class="info-item-1">本文讲述如何给 hexo 博客网站配置。 1. 安装环境说明 首先粘贴本教程测试使用的环境，如果在安装配置中，环境有差异，请酌情修改或调整参数．  1234567891011C:\JetBrain\WebStorm\MyBlog\blog-demo-03&gt;pnpm listLegend: production dependency, optional only, dev onlyhexo-site@0.0.0 Z:\JetBrain\WebStorm\MyBlog\blog-demo-03 (PRIVATE)dependencies:@babel/preset-env 7.26.0       hexo-generator-index 3.0.0     hexo-renderer-marked 6.3.0babel-plugin-inferno 6.7.2     hexo-generator-tag 2.0.0       hexo-renderer-stylus 3.0.1hexo 7.3.0                     hexo-log 4.1.0  ...</div></div></div></a></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info text-center"><div class="avatar-img"><img src="/img/avatar.png" onerror="this.onerror=null;this.src='/img/loading.gif'" alt="avatar"/></div><div class="author-info-name">David</div><div class="author-info-description">Welcome to David's Blog</div><div class="site-data"><a href="/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">28</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">28</div></a></div><a id="card-info-btn" target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/david-deng-01"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons"><a class="social-icon" href="https://github.com/david-deng-01" rel="external nofollow noreferrer" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://blog.csdn.net/David_0925" rel="external nofollow noreferrer" target="_blank" title="CSDN"><i class="fa fa-book-open"></i></a><a class="social-icon" href="tencent://AddContact/?fromId=45&amp;fromSubId=1&amp;subcmd=all&amp;uin=635647792&amp;website=www.oicqzone.com" rel="external nofollow noreferrer" target="_blank" title="QQ"><i class="fab fa-qq"></i></a><a class="social-icon" href="mailto:david-deng-0925@qq.com" rel="external nofollow noreferrer" target="_blank" title="Email"><i class="fas fa-envelope-open-text"></i></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#2023-08-31-%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0"><span class="toc-number">1.</span> <span class="toc-text">2023-08-31 学习笔记</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E9%A2%84%E8%AE%AD%E7%BB%83%E8%AF%AD%E8%A8%80%E6%A8%A1%E5%9E%8B%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">1.1.</span> <span class="toc-text">1. 预训练语言模型的使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%97%AE%E9%A2%98%EF%BC%9A"><span class="toc-number">1.1.1.</span> <span class="toc-text">问题：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%E5%A6%82%E4%B8%8B%EF%BC%9A"><span class="toc-number">1.1.2.</span> <span class="toc-text">解决方案如下：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E5%A6%82%E4%B8%8B%EF%BC%9A"><span class="toc-number">1.1.3.</span> <span class="toc-text">代码如下：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%8C%E6%95%B4%E8%AE%AD%E7%BB%83%E4%BB%A3%E7%A0%81%E5%A6%82%E4%B8%8B%EF%BC%9A"><span class="toc-number">1.1.4.</span> <span class="toc-text">完整训练代码如下：</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E5%8F%91%E7%8E%B0%E6%96%B0%E7%9A%84%E8%BD%AF%E4%BB%B6%E4%BD%BF%E7%94%A8%E6%8A%80%E5%B7%A7"><span class="toc-number">1.2.</span> <span class="toc-text">2. 发现新的软件使用技巧</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E9%94%99%E8%AF%AF%E8%A7%A3%E5%86%B3"><span class="toc-number">1.3.</span> <span class="toc-text">3. 错误解决</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2025/01/05/other-%E9%9A%8F%E7%AC%94-icarus-%E4%B8%BB%E9%A2%98%E5%AE%89%E8%A3%85/" title="Hexo 配置 Icarus 主题"><img src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-4.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Hexo 配置 Icarus 主题"/></a><div class="content"><a class="title" href="/2025/01/05/other-%E9%9A%8F%E7%AC%94-icarus-%E4%B8%BB%E9%A2%98%E5%AE%89%E8%A3%85/" title="Hexo 配置 Icarus 主题">Hexo 配置 Icarus 主题</a><time datetime="2025-01-05T08:25:00.000Z" title="发表于 2025-01-05 16:25:00">2025-01-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/03/18/other-leetcode-%E3%80%902024-03-18-LeetCode-%E5%88%B7%E9%A2%98-400%E9%81%93%E6%89%93%E5%8D%A1%E3%80%91/" title="LeetCode刷题400道打卡"><img src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="LeetCode刷题400道打卡"/></a><div class="content"><a class="title" href="/2024/03/18/other-leetcode-%E3%80%902024-03-18-LeetCode-%E5%88%B7%E9%A2%98-400%E9%81%93%E6%89%93%E5%8D%A1%E3%80%91/" title="LeetCode刷题400道打卡">LeetCode刷题400道打卡</a><time datetime="2024-03-17T16:00:00.000Z" title="发表于 2024-03-18 00:00:00">2024-03-18</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/01/04/2024-01-05%20%E5%A4%9A%E6%A8%A1%E6%80%81%E6%A8%A1%E5%9E%8B%E7%BB%BC%E8%BF%B0/" title="多模态基础模型：从专家到通用助理"><img src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-7.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="多模态基础模型：从专家到通用助理"/></a><div class="content"><a class="title" href="/2024/01/04/2024-01-05%20%E5%A4%9A%E6%A8%A1%E6%80%81%E6%A8%A1%E5%9E%8B%E7%BB%BC%E8%BF%B0/" title="多模态基础模型：从专家到通用助理">多模态基础模型：从专家到通用助理</a><time datetime="2024-01-04T14:42:08.000Z" title="发表于 2024-01-04 22:42:08">2024-01-04</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/12/30/%E6%A0%A1%E5%9B%AD%E7%BD%91%E8%87%AA%E5%8A%A8%E7%99%BB%E5%BD%95%E8%84%9A%E6%9C%AC/" title="校园网自动登录脚本"><img src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="校园网自动登录脚本"/></a><div class="content"><a class="title" href="/2023/12/30/%E6%A0%A1%E5%9B%AD%E7%BD%91%E8%87%AA%E5%8A%A8%E7%99%BB%E5%BD%95%E8%84%9A%E6%9C%AC/" title="校园网自动登录脚本">校园网自动登录脚本</a><time datetime="2023-12-30T09:21:30.000Z" title="发表于 2023-12-30 17:21:30">2023-12-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/11/24/%E5%A4%9A%E6%A8%A1%E6%80%81%E8%AE%BD%E5%88%BA%E8%AF%86%E5%88%AB%E5%9F%BA%E7%BA%BF%E6%A8%A1%E5%9E%8B%E5%A4%8D%E7%8E%B0/" title="多模态讽刺识别基线模型复现"><img src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-10.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="多模态讽刺识别基线模型复现"/></a><div class="content"><a class="title" href="/2023/11/24/%E5%A4%9A%E6%A8%A1%E6%80%81%E8%AE%BD%E5%88%BA%E8%AF%86%E5%88%AB%E5%9F%BA%E7%BA%BF%E6%A8%A1%E5%9E%8B%E5%A4%8D%E7%8E%B0/" title="多模态讽刺识别基线模型复现">多模态讽刺识别基线模型复现</a><time datetime="2023-11-24T08:51:18.000Z" title="发表于 2023-11-24 16:51:18">2023-11-24</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2025 By David</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><a href="http://www.beian.gov.cn/portal/registerSystemInfo" rel="external nofollow noreferrer" target="_blank"> <img style="vertical-align:middle; width:20px; " src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/icp.png"> 赣公网安备36082302000135号</a> <a href="https://beian.miit.gov.cn/" rel="external nofollow noreferrer" id="beian"  target="_blank">赣ICP备2023013705号-1</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="日间和夜间模式切换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><div class="js-pjax"></div><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>